// index.js
// 获取应用实例
const app = getApp()

// 引入接口
const {getList}=require('../../api/index')
const request = require('../../utils/request')
Page({
  data: {
    motto: '允许获取微信信息',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        // console.log(res.userInfo,'res.yserinfo')
        wx.setStorageSync("userInfo", res.userInfo)
        //获取appid
        const appid = wx.getAccountInfoSync().miniProgram.appId
        // console.log(appid,'appid')
        //secret
        const secret = '2d7b1e5cf4bcb5a816012e9f6f5196da'; 
        const grant_type = 'authorization_code';
        //获取code
        wx.login({
          success: (result) => {
            console.log(result, 'wx.login-result')
            const code = result.code
            wx.request({
              url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`,
              success: (rs => {
                // console.log(rs, 'openid')
                wx.setStorageSync('openid',rs.data.openid)
                this.setData({
                  openId:rs.data.openid
                })
                this.getWxUser(res,rs)
              })
            })
          }
        })
      }
    })
  },
   //调用微信小程序用户接口
  getWxUser(res,rs){
    console.log(res,rs,'this.data.openId')
    request({
      url: '/api/wxuser',
      method: 'post',
      data: {
        nickName: res.userInfo.nickName,
        avatarUrl: res.userInfo.avatarUrl,
        openId: rs.data.openid
      },
    }).then(res=>{
<<<<<<< HEAD
      // console.log(res,'res')
      wx.setStorageSync('userInfo',res.userInfo)
=======
      console.log(res,'res')
      wx.setStorageSync('userInfo',res.data.userInfo)
>>>>>>> 71b55d2960bfa219464f53ca38ddfabec18b55fc
      wx.switchTab({
        url: '../home/index',
      })
    })
  },
  
})